<?php

/**
 * oRs
 *
 * @version 3.0
 * @copyright 2009 Ron Planken
 * @class: Database
 * @description: Handles all database communication
 */

class database
{

				private $_databaseserver;
				private $_username;
				private $_password;
				private $_databasename;

				private $_util;
				
				private $_dbh;
				
				function __construct($util) {

				 	$this->_util = $util;
					
				 	$this->_databaseserver = config::_DB_DATABASESERVER;
				 	$this->_username = config::_DB_USERNAME;
				 	$this->_password = config::_DB_PASSWORD;
				 	$this->_databasename = config::_DB_DATABASENAME;
				 	
				 	$this->initConnection();
				}

				function initConnection()
				{
				    $dbh = mysql_connect ($_databaseserver, $_username, $_password) or die ('I cannot connect to the database because: ' . mysql_error());
				    
				    mysql_select_db($_databasename);
				    
				    $this->_dbh = $dbh; 
				}

				function UpdateQuery($query){
					
					 $this->_util->debug_print($query);
					
			     $result = mysql_query($query, $this->_dbh);
				 	 $num_rows = mysql_affected_rows();
			     return $num_rows; 	
				}						
				
				function selectMultiQuery($query) //returns an integer based array
				{
					$this->_util->startTimer();	
										
			    $arr = array();
			    $result = mysql_query($query, $this->_dbh);
			    while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
			        $arr[] = $row;
			
			    }
					$this->_util->stopTimer();
	
				  $this->_util->debug_print($query, $this->_util->calculateTimer());
				    
				  return $arr;
				}
				
				
				function selectRowQuery($query) //returns the top row
				{
					
					$this->_util->debug_print($query);
					
				  $result = mysql_query($query,  $this->_dbh);
					$row = mysql_fetch_array($result, MYSQL_BOTH);
				
				  return $row;
				}
				
				function selectMultiTextQuery($query) //returns an text based array
				{
					$this->_util->startTimer();	
					
				  $arr = array();
				  $result = mysql_query($query, $dbh);
				  while ($row = mysql_fetch_array($result)) {
				      $arr[$row[0]] = $row;
				  }
				    
					$this->_util->stopTimer();
	
				  $this->_util->debug_print($query, $this->_util->calculateTimer());
 
				  return $arr;
				}
				
				
				function selectMultiRpQuery($query) //returns an text based array
				{
						$this->_util->startTimer();
						
				    $arr = array();
				    $result = mysql_query($query, $dbh);
				    while ($row = mysql_fetch_array($result)) {
				        $arr[$row[0]][$row[1]] = $row;
				    }
				    
					  $this->_util->stopTimer();
	
				    $this->_util->debug_print($query, $this->_util->calculateTimer());
 				    
				    return $arr;
				}
				
				function selectSingleTextQuery($query) //returns an text based array
				{
					  $this->_util->debug_print($query);
					
				    $arr = array();
				    $result = mysql_query($query, $dbh);
				    
				    while ($row = mysql_fetch_array($result)) {
				        $arr[$row[0]] = $row[1];
				    }
				    return $arr;
				}
				
				function selectQuery($query, $dbh)
				{
					  $this->_util->debug_print($query);
					
				    $result = mysql_query($query);
				
				    $row = mysql_fetch_row($result);
				
				    return $row[0];
				}
				
				function InsertQuery($query, $dbh)
				{
					  $this->_util->debug_print($query);
						 
				    $result = mysql_query($query, $dbh);
				    
				    
				    $status = mysql_insert_id($dbh);
				    
				    return $status; //returned FALSE/TRUE als de query succesvol is uitgevoerd
				}
				 
				public function secure_news($string){
					$string = mysql_real_escape_string(stripslashes($string));
					return "'" . $string . "'";
				}
				
				public function secure_string($string){
					$string = mysql_real_escape_string(htmlspecialchars(stripslashes($string)));
					return "'" . $string . "'";
				}
				
				public function secure_int($int){
					$int = abs($int);
					$int = mysql_real_escape_string(stripslashes($int));
					return $int;
				}
				
				function destroyConnection()
				{
				    mysql_close();
				}

}
?>